package cn.edu.xhu.library_management.department.controller;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.department.entity.LmClass;
import cn.edu.xhu.library_management.department.entity.search.SearchClass;
import cn.edu.xhu.library_management.department.entity.vo.ClassVo;
import cn.edu.xhu.library_management.department.service.ClassService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022-02-02
 * @Description: 班级controller
 */
@Api(tags = "班级模块")
@ApiSort(value = 3)
@RestController
@RequestMapping("/lm_department")
public class ClassController {

    @Autowired
    private ClassService classService;

    // 查询所有班级
    @ApiOperation(value = "查询所有班级",produces = "application/json")
    @ApiOperationSupport(order = 311)
    @GetMapping("/class")
    public R findAllClasses(){
        List<LmClass> classes = classService.findAllClasses();
        return !classes.isEmpty()?R.ok().data("classes",classes):R.error("无数据!!!");
    }

    // 查询专业对应的班级
    @ApiOperation(value = "查询专业对应的班级",produces = "application/json")
    @ApiOperationSupport(order = 312)
    @GetMapping("/class/majors/{maId}")
    public R findClassesByMajor(@PathVariable("maId") Integer maId){
        List<ClassVo> classes = classService.findClassesByMajor(maId);
        return !classes.isEmpty()?R.ok().data("classes",classes):R.error("无数据!!!");
    }

    // 通过班级ID查询班级
    @ApiOperation(value = "通过班级ID查询班级",produces = "application/json")
    @ApiOperationSupport(order = 313)
    @GetMapping("/class/{clId}")
    public R findClassById(@PathVariable("clId") Integer clId){
        ClassVo classVo = classService.findClassById(clId);
        return classVo!=null ? R.ok().data("class",classVo):R.error("查无此数据!!!");
    }

    @ApiOperation(value = "搜索班级",produces = "application/json")
    @ApiOperationSupport(order = 314)
    @GetMapping("/class/search")
    public R search(SearchClass searchClass){
        Map<String, Object> search = classService.search(searchClass);
        return R.ok().data(search);
    }

    // 添加班级
    @ApiOperation(value = "添加班级",produces = "application/json")
    @ApiOperationSupport(order = 321)
    @PostMapping("/class")
    public R addClass(@RequestBody LmClass lmClass){
        return classService.addClass(lmClass)?R.ok("添加班级成功!!!"):R.error("添加班级失败!!!");
    }

   // 修改班级(班级ID)
    @ApiOperation(value = "修改班级(班级ID)",produces = "application/json")
    @ApiOperationSupport(order = 331)
    @PutMapping("/class")
    public R updateClass(@RequestBody LmClass lmClass){
        return classService.updateClass(lmClass)?R.ok("修改班级成功!!!"):R.error("修班级失败!!!");
    }

    // 通过班级ID，删除班级
    @ApiOperation(value = "通过班级ID，删除班级",produces = "application/json")
    @ApiOperationSupport(order = 341)
    @DeleteMapping("/class/{clId}")
    public R deleteClassById(@PathVariable("clId") Integer clId){
        return classService.deleteClassById(clId)?R.ok("删除班级成功!!!"):R.error("删除班级失败!!!");
    }

    // 通过班级IDs，删除班级
    @ApiOperation(value = "通过班级IDs，删除班级",produces = "application/json")
    @ApiOperationSupport(order = 342)
    @DeleteMapping("/class")
    public R deleteClassesByIds(@RequestParam("clIds") List<Integer> clIds){
        return classService.deleteClassesByIds(clIds)?R.ok("批量删除班级成功!!!"):R.error("批量删除班级失败!!!");
    }
}
